import Vue from "vue";

// 检查paypal充值状态
export const checkPaypalRecharge = (option) => {
    return Vue.prototype
        .$http("Wallet.getRechargeState/", option.polldate)
        .then((response) => {
            const data = response.data || {};
            const status = {
                1: "pending",
                2: "delayed",
                20: "failure",
                0: "success",
                10: "success",
                100: "polling", // recharging 充值中
                8100: "failure",
            }[data.code];

            if (status) {
                const msg = {
                    20: "paypal recharge failed",
                    8100: "paypal recharge failed",
                };
                return Promise.resolve({
                    status: status,
                    message: msg[data.code] || response.msg,
                });
            } else {
                return Promise.reject({
                    status: "failure",
                    message: "Unusual order status",
                });
            }
        });
};

// 检查卡充值状态
export const checkCardRecharge = (option) => {
    return Vue.prototype
        .$http("Order.getCardRechargeState/", option.polldate)
        .then((response) => {
            // recharge_code=2：pending 状态
            // recharge_code=10：complete 状态
            // recharge_code=20：failed 状态

            const data = response.data || {};
            const status = {
                1: "pending",
                2: "delayed",
                10: "success",
                20: "failure",
                100: "polling",
            }[data.recharge_code];

            if (status) {
                const msg = { 20: "paypal recharge failed" };
                return Promise.resolve({
                    status: status,
                    message: msg[data.code] || response.msg,
                });
            } else {
                return Promise.reject({
                    status: "failure",
                    message: "Unusual order status",
                });
            }
        });
};

// 检查订单支付状态
export const checkSingleStatus = (option) => {
    // 单个支付查询
    let request = { order_codes: [option.codes] };
    return Vue.prototype
        .$http("Order.getOrderStatus", request)
        .then((response) => {
            // order_state=1：支付中
            // order_state=2：已支付

            // 订单支付状态
            // order_pay_state=1：pending，
            // order_pay_state=2：delayed，
            // order_pay_state=10或0：complete
            // order_pay_state=20：failed

            const data = response.data || [];
            const states = data.filter((i) => i.order_code == option.codes);
            const active = states[0] || {};

            if (active.order_state == 2) {
                // 成功
                // 订单支付下，pending或delayed都是支付成功

                // const status = {
                //     0: "polling",
                //     1: "pending",
                //     2: "delayed",
                //     10: "success",
                //     20: "failure",
                // };

                return Promise.resolve({
                    status: "success",
                    message: response.msg || "",
                });
            } else if (active.order_state == 1) {
                // 支付中
                const status = {
                    0: "polling",
                    10: "success",
                    20: "failure",
                };

                const message = {
                    0: response.msg,
                    10: response.msg,
                    20: "Order pay failure!",
                };

                return Promise.resolve({
                    status: status[active.order_pay_state],
                    message: message[active.order_pay_state],
                });
            } else {
                // 其他情况
                return Promise.reject({
                    status: "failure",
                    message: "Unusual order status",
                });
            }
        });
};

// 检查批量支付状态，目前只有钱包能批量支付
export const checkBatchStatus = (option) => {
    // 批量支付查询
    let payment = option.payment;
    let paycode = { paypal: 1, card: 2, payoneer: 3, wallet: 4 }[payment];
    let request = {
        order_code: option.codes,
        payment: paycode,
        batch_label: option.batch_label || "",
    };
    return Vue.prototype
        .$http("Order.batchPayStatus", request)
        .then((response) => {
            const status = {
                1: "failure",
                0: "success",
                100: "polling",
            }[response.code];

            if (!status) {
                return Promise.reject({
                    status: "failure",
                    message: "Unusual order status",
                });
            } else if (status == "failure") {
                // 批量支付失败
                let data = [];
                try {
                    data = JSON.parse(response.data || "[]");
                } catch (err) {}
                return Promise.resolve({
                    status: status,
                    data: data && data.length > 0 ? data : [],
                    message: response.msg || data,
                });
            } else {
                return Promise.resolve({
                    status: status,
                    message: response.msg,
                });
            }

            // if (response.code === 1) {
            // 	// 失败
            // 	return Promise.resolve({
            // 		status: "failure",
            // 		message: "Please check your network connection",
            // 	});

            // 	// this.dialog &&
            // 	//     this.dialog.callback(false, {
            // 	//         status: "paying",
            // 	//         type: "error",
            // 	//         message: "Please check your network connection",
            // 	//     });
            // } else if (response.code === 0) {
            // 	// 成功
            // 	// this.dialog && this.dialog.callback(true);

            // 	return Promise.resolve({
            // 		status: 'success',
            // 		message: response.msg,
            // 	});

            // } else {
            // 	// 未知情况，可能是网络失败
            // 	return Promise.reject({
            // 		status: "failure",
            // 		message: "Unusual order status",
            // 	});

            // 	// code === 100 表示接着轮询
            // 	// this.timer = setTimeout(this.payPolling, 1000);
            // }
        });
};

// 获取订单状态
export const checkPaymentStatus = (option) => {
    if (option.action == "batch") {
        return checkBatchStatus(option);
    } else if (option.action == "single") {
        return checkSingleStatus(option);
    } else if (option.action == "recharge" && option.payby == "paypal") {
        return checkPaypalRecharge(option);
    } else if (option.action == "recharge" && option.payby == "card") {
        return checkCardRecharge(option);
    } else {
        return Promise.resolve("unknow payment");
    }
};

export default {
    checkPaypalRecharge,
    checkCardRecharge,
    checkSingleStatus,
    checkBatchStatus,
    checkPaymentStatus,
};
